package com.example.haohao.myview.lines;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;

/**
 * 绘制 渐变折线图
 * 核心思路，使用path绘制折线区域，path1
 * 再调用LinearGradient 线性渐变，绘制折线区域，path2
 * 如果想要显示一条线，需要有差距PathDistance
 */
public class LineGraphs extends View {

    /**
     *
     */

    int realWidth;
    int realHeight;
    int defaultWidth=400;
    int defaultHeight=400;
    int PathDistance=5;

    ArrayList<Node> nodeList=new ArrayList<>();

    public LineGraphs(Context context) {
        this(context,null);
    }

    public LineGraphs(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LineGraphs(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 1 判断宽度填充方式
        int widthMode=MeasureSpec.getMode(heightMeasureSpec);
        switch (widthMode) {
            case MeasureSpec.UNSPECIFIED:
                realWidth=defaultWidth;
                break;
            case MeasureSpec.EXACTLY:
                realWidth=widthMeasureSpec;
                break;
            case MeasureSpec.AT_MOST:
                realWidth=Math.min(defaultWidth,widthMeasureSpec);
                break;

        }
//        2,判定高度填充方式
         int heightMode=MeasureSpec.getMode(heightMeasureSpec);
        switch (heightMode) {
            case MeasureSpec.UNSPECIFIED:
                realHeight=defaultHeight;
                break;
            case MeasureSpec.EXACTLY:
                realHeight=heightMeasureSpec;
                break;
            case MeasureSpec.AT_MOST:
                realHeight=Math.min(heightMeasureSpec,defaultHeight);
                break;

        }
//        3，设置宽高给画布
        setMeasuredDimension(realWidth, realHeight);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Path linePath=new Path();
        Path mShadowPath=new Path();
        if (nodeList!=null && nodeList.size()>1) {
//            起点
            linePath.moveTo(0,getHeight()-PathDistance);
            mShadowPath.moveTo(0,getHeight());

            for (int i = 0; i < nodeList.size(); i++) {

//                采用drawLine绘制一条折线，需要注意集合下标溢出的问题
//                canvas.drawLine(nodeList.get(i).getX(), nodeList.get(i).getY(),
//                        nodeList.get(i+1).getX(), nodeList.get(i+1).getY(),
//                        paint);

                linePath.lineTo(nodeList.get(i).getX(), nodeList.get(i).getY() - PathDistance);
                mShadowPath.lineTo(nodeList.get(i).getX(), nodeList.get(i).getY());

            }
//            终点
            linePath.lineTo(nodeList.get(nodeList.size()-1).getX(), 400-PathDistance);
            mShadowPath.lineTo(nodeList.get(nodeList.size()-1).getX(), 400);



            int[] colorse={Color.RED,Color.YELLOW};
            LinearGradient linearGradient2 = new LinearGradient(0,0,0,getHeight(),colorse,null, Shader.TileMode.CLAMP);

            Paint paint =new Paint();
            paint.setColor(Color.BLUE);
            canvas.drawPath(linePath,paint);

            Paint paint2=new Paint();
            paint2.setShader(linearGradient2);
            canvas.drawPath(mShadowPath,paint2);
        }

    }

    /**
     * 设置需要绘制的点，再刷新一下
     * @param nodelist 需要绘制的点
     */
    public void setNodeData(ArrayList<Node> nodelist){
       this.nodeList=nodelist;
        postInvalidate();
    }


}
